package com.syh.jn.scm.service.impl.downstream.finance;

import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.event.annotation.DomainEventHandle;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.finance.DownPaymentBill;
import com.syh.jn.scm.domain.downstream.entity.finance.DownStatementAccountBill;
import com.syh.jn.scm.domain.downstream.event.DownPaymentBillEvent;
import com.syh.jn.scm.domain.downstream.event.DownStatementAccountBillEvent;
import com.syh.jn.scm.domain.downstream.repository.finance.DownStatementAccountBillRepository;
import com.syh.jn.scm.dto.downstream.finance.DownStatementAccountBillItemVo;
import com.syh.jn.scm.dto.downstream.finance.DownStatementAccountBillMto;
import com.syh.jn.scm.dto.downstream.finance.DownStatementAccountBillSto;
import com.syh.jn.scm.dto.downstream.finance.DownStatementAccountBillVo;
import com.syh.jn.scm.dto.support.FilesDto;
import com.syh.jn.scm.service.api.downstream.finance.DownStatementAccountBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Date:2018/7/19
 * Author:Clown
 * 下游对账单service实现;
 */
@Service
@Transactional
public class DownStatementAccountBillServiceImpl extends AbstractServiceImpl<DownStatementAccountBillSto, DownStatementAccountBillMto, DownStatementAccountBillVo, DownStatementAccountBill, Long> implements DownStatementAccountBillService {
  @Autowired
  private DownStatementAccountBillRepository downstreamStatementAccountBillRepositoryImpl;

  @Override
  protected AggregateRepository<DownStatementAccountBill, Long> getAggregateRepository() {
    return downstreamStatementAccountBillRepositoryImpl;
  }

  /**
   * 上传单据图片
   *
   * @param dto
   */
  @Override
  public void upload(FilesDto dto) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(dto.getId());
    bill.upload(dto);
    downstreamStatementAccountBillRepositoryImpl.upload(bill);
  }

  /**
   * 根据对账单id获取明细
   *
   * @param statementId 对账单id
   * @return
   */
  @Override
  public List<DownStatementAccountBillItemVo> getItemsByStatementId(Long statementId) {
    return downstreamStatementAccountBillRepositoryImpl.getItemsByStatementId(statementId);
  }

  /**
   * 保存下游对账单
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(DownStatementAccountBillSto sto) {
    DownStatementAccountBill bill = new DownStatementAccountBill(sto);
    Long id = downstreamStatementAccountBillRepositoryImpl.add(bill);
    bill.setId(id);
    this.publicEvent(DownStatementAccountBillEvent.bulidDownStatementBillAccountCreated(new Object[]{bill}));
    return id;
  }

  /**
   * 修改下游对账单
   * @param mto
   */
  @Override
  public void update(DownStatementAccountBillMto mto) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(mto.getId());
    bill.modify(mto);
    downstreamStatementAccountBillRepositoryImpl.update(bill);
    this.publicEvent(DownStatementAccountBillEvent.bulidDownStatementBillAccountUpdata(new Object[]{bill}));
  }

  /**
   * 删除下游对账单
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(id);
    bill.destroy();
    this.publicEvent(DownStatementAccountBillEvent.bulidDownStatementBillAccountDestory(new Object[]{bill}));
    //删除
    downstreamStatementAccountBillRepositoryImpl.remove(bill);
  }

  /**
   * 业务确认
   *
   * @param id
   */
  @Override
  public void businessConfirmation(Long id) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(id);
    bill.businessConfirmation();
    downstreamStatementAccountBillRepositoryImpl.update(bill);
  }

  /**
   * 业务取消确认
   *
   * @param id
   */
  @Override
  public void businessCancelConfirmation(Long id) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(id);
    bill.businessCancelConfirmation();
    downstreamStatementAccountBillRepositoryImpl.update(bill);
  }

  /**
   * 财务确认
   *
   * @param id
   */
  @Override
  public void financeConfirmation(Long id) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(id);
    bill.financeConfirmation();
    downstreamStatementAccountBillRepositoryImpl.update(bill);
  }

  /**
   * 财务取消确认
   *
   * @param id
   */
  @Override
  public void financeCancelConfirmation(Long id) {
    DownStatementAccountBill bill = downstreamStatementAccountBillRepositoryImpl.getById(id);
    bill.financeCancelConfirmation();
    downstreamStatementAccountBillRepositoryImpl.update(bill);
  }

  /**
   * 消费者：监听付款单（修改付款到账金额）
   *
   * @param bill
   */
  @DomainEventHandle(DownPaymentBillEvent.DOWN_PAYMENT_BILL_LOCK)
  protected void handleDownStatementBillAccountPayEvent(DownPaymentBill bill) {
    DownStatementAccountBill billState = downstreamStatementAccountBillRepositoryImpl.getById(bill.getStatementOfAccountId());
    billState.accountAmount(bill.getPrice());
    downstreamStatementAccountBillRepositoryImpl.update(billState);
  }

  /**
   * 消费者：监听付款单（修改付款到账金额和收款状态）
   *
   * @param bill
   */
  @DomainEventHandle(DownPaymentBillEvent.DOWN_PAYMENT_BILL_UNLOCK)
  protected void handleDownStatementBillAccountPayUnLockEvent(DownPaymentBill bill) {
    DownStatementAccountBill billState = downstreamStatementAccountBillRepositoryImpl.getById(bill.getStatementOfAccountId());
    billState.subAccountAmount(bill.getPrice());
    downstreamStatementAccountBillRepositoryImpl.update(billState);
  }

  /**
   * 消费者：监听付款单（修改已生成付款金额）
   *
   * @param bill
   */
  @DomainEventHandle(DownPaymentBillEvent.DOWN_PAYMENT_BILL_CREATED)
  protected void handleDownStatementBillAccountPayCreatedEvent(DownPaymentBill bill) {
    DownStatementAccountBill billState = downstreamStatementAccountBillRepositoryImpl.getById(bill.getStatementOfAccountId());
    billState.addGeneratedCollectionsPrice(bill.getPrice());
    downstreamStatementAccountBillRepositoryImpl.update(billState);
  }

  /**
   * 消费者：监听付款单（修改已生成付款金额）
   *
   * @param bill
   */
  @DomainEventHandle(DownPaymentBillEvent.DOWN_PAYMENT_BILL_DESTORY)
  protected void handleDownStatementBillAccountPayDestoryEvent(DownPaymentBill bill) {
    DownStatementAccountBill billState = downstreamStatementAccountBillRepositoryImpl.getById(bill.getStatementOfAccountId());
    billState.subGeneratedCollectionsPrice(bill.getPrice());
    downstreamStatementAccountBillRepositoryImpl.update(billState);
  }

  /**
   * 消费者：监听开票（修改开票金额）
   * @param bill
   */
  //TODO 暂时在服务端监听
/*  @DomainEventHandle(DownStatementAccountBillEvent.DOWNSTREAM_STATEMENT_BILL_INVOICE_DESTORY)
  protected void handleDownStatementBillInvoiceEvent(DownStatementAccountBill bill) {
    DownStatementAccountBill billState = downstreamStatementAccountBillRepositoryImpl.getById(bill.getId());
    billState.invoiceValue(bill.getTicketPrice());
    downstreamStatementAccountBillRepositoryImpl.update(billState);
  }*/
}
